import json
import os

import wx

FRAME_WIDTH = 1000
FRAME_HEIGHT = 500
# 字体大小
FONT_SIZE = 14
READ_INTERVAL_MILLS = 1000


class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, pos=(-1, -1), title=title, size=(FRAME_WIDTH, FRAME_HEIGHT))
        self.cfgItems ={} #形如{0:()}
        self.curPosition = 0  # 当前位置索引
        self.initView()
        self.cfgWindow.resetContent()

    def initView(self):
        self.initMenuBar()
        self.initLayout()

    def initMenuBar(self):
        self.menubar = wx.MenuBar()

        menu1 = wx.Menu()
        versionItem = menu1.Append(-1, "操作说明")
        self.Bind(wx.EVT_MENU, self.showHelp, versionItem)

        menu2 = wx.Menu()
        saveItem = menu2.Append(wx.ID_SAVE, "保存配置")
        self.Bind(wx.EVT_MENU, self.save, saveItem)

        # addItem = menu2.Append(wx.ID_ADD, "添加")
        # self.Bind(wx.EVT_MENU,self.addItem,addItem)
        # removeItem = menu2.Append(wx.ID_REMOVE, "移除")
        # self.Bind(wx.EVT_MENU,self.removeItem,removeItem)

        self.menubar.Append(menu1, "帮助")
        self.menubar.Append(menu2, "操作")
        self.SetMenuBar(self.menubar)

        self.statusBar = wx.StatusBar(self, -1)  # 实例化 wx.StatusBar
        self.statusBar.SetFieldsCount(3)  # 状态栏分成3个区域
        self.statusBar.SetStatusWidths([-1, -1, -1])  # 区域宽度比列，用负数
        self.statusBar.SetStatusText("", 0)  # 给状态栏设文字
        self.SetStatusBar(self.statusBar)  # 将状态栏附加到框架上

    def initLayout(self):
        self.rootLayout = wx.BoxSizer(wx.HORIZONTAL)

        preBox = wx.BoxSizer(wx.VERTICAL)
        tv = wx.StaticText(self, -1, "数据预览")
        tv.SetForegroundColour("0xff0000")
        font = self.GetFont()
        font.SetPointSize(20)
        tv.SetFont(font)
        self.et_preview = wx.TextCtrl(self, -1, "", size=(400, 500), style=wx.TE_MULTILINE)
        self.et_preview.SetEditable(False)
        preBox.Add(tv, 0, wx.LEFT, border=20)
        preBox.Add(self.et_preview, 0, wx.LEFT, border=20)

        leftBox = wx.BoxSizer(wx.VERTICAL)
        btnBox = wx.BoxSizer(wx.HORIZONTAL)
        self.preBtn = wx.Button(self, -1, "上一个", size=(100, 40))
        self.nextBtn = wx.Button(self, -1, "下一个", size=(100, 40))
        self.preBtn.Bind(wx.EVT_BUTTON, self.preItem)
        self.nextBtn.Bind(wx.EVT_BUTTON, self.nextItem)

        btnBox.Add(self.preBtn, 0, wx.LEFT, border=20)
        btnBox.Add(self.nextBtn, 0, wx.LEFT, border=150)
        leftBox.Add(btnBox, 0, wx.EXPAND | wx.TOP, border=20)

        self.cfgWindow = ConfigWindow(self)
        leftBox.Add(self.cfgWindow, 0, wx.EXPAND | wx.TOP, border=20)

        self.okBtn = wx.Button(self, -1, "确定修改", size=(80, 30))
        leftBox.Add(self.okBtn, 0, wx.LEFT, border=300)
        self.okBtn.Bind(wx.EVT_BUTTON, self.saveItem)

        self.rootLayout.Add(leftBox, 0, wx.EXPAND | wx.TOP, border=0)
        self.rootLayout.Add(preBox, 0, wx.LEFT | wx.TOP, border=20)
        self.SetSizer(self.rootLayout)
        self.SetBackgroundColour((250, 250, 250))

    def initData(self):
        pass

    def showHelp(self,e):
        str='''
        1、点击“选择视频文件”
        2、填写相关标题和内容介绍
        3、点击“确定修改”
        4、如需增加配置项点击“下一个”，重复步骤1-3
        5、菜单栏-操作-保存配置
        6、将localAd.txt配置文件拷贝到广告机的根目录/fatri/Ad文件夹下
           
        '''
        wx.MessageBox(str,caption="帮助说明")
        pass

    def nextItem(self, e):
        self.curPosition+=1
        if self.curPosition>len(self.cfgItems):
            self.curPosition-=1
            wx.MessageBox("请填写当前配置项")
            return
        print("postion:",str(self.curPosition))


        if self.curPosition<len(self.cfgItems):
            self.cfgWindow.loadContent(self.cfgItems[self.curPosition])
        else:
            self.cfgWindow.resetContent()
        pass

    def preItem(self, e):
        if self.curPosition==0:
            return
        self.curPosition-=1
        item=self.cfgItems[self.curPosition]
        self.cfgWindow.loadContent(item)
        pass

    def saveItem(self, e):
        item= self.cfgWindow.getItemDict()  # (key ,dict)
        if item[0]=="":
            wx.MessageBox("请选择视频文件")
            return
        self.cfgItems[self.curPosition] =item
        self.formatConfig()
        wx.MessageBox("修改成功")

    def save(self,e):
        data=self.et_preview.GetValue()
        if data=="":
            wx.MessageBox("数据为空，保存失败")
            return
        f=open('localAd.txt','w',encoding='utf-8')
        f.write(data)
        f.close()
        wx.MessageBox("已保存到当前目录localAd.txt")
        pass

    def formatConfig(self):
        dict={}
        for k in self.cfgItems.keys():
            tuple=self.cfgItems.get(k)
            dict[tuple[0]]=tuple[1]

        jsonStr=json.dumps(dict,ensure_ascii=False)
        self.et_preview.SetValue(jsonStr)

class ConfigWindow(wx.BoxSizer):
    def __init__(self, ctx):
        super().__init__(orient=wx.VERTICAL)
        self.editing=True

        fileBox = wx.BoxSizer(wx.HORIZONTAL)
        self.et_file = wx.TextCtrl(ctx, -1, value="", size=(300, 25))
        self.et_file.SetEditable(False)
        self.btn_select_file = wx.Button(ctx, -1, "选择视频文件", size=(100, 25))
        self.btn_select_file.Bind(wx.EVT_BUTTON, self.selectFile)
        fileBox.Add(self.et_file, 0, flag=wx.EXPAND | wx.LEFT, border=20)
        fileBox.Add(self.btn_select_file, 0, flag=wx.EXPAND | wx.LEFT, border=20)
        self.Add(fileBox, 0, wx.EXPAND)

        titleBox = wx.BoxSizer(wx.HORIZONTAL)
        titleBox.Add(wx.StaticText(ctx, -1, "标题:", size=(60, 25)), 0, flag=wx.EXPAND | wx.LEFT, border=20)
        self.et_title = wx.TextCtrl(ctx, -1, value="", size=(300, 25))
        titleBox.Add(self.et_title, 0, flag=wx.EXPAND | wx.LEFT, border=0)
        self.Add(titleBox, 0, wx.EXPAND | wx.TOP, border=10)

        contentBox = wx.BoxSizer(wx.HORIZONTAL)
        contentBox.Add(wx.StaticText(ctx, -1, "内容介绍:", size=(60, 25)), 0, flag=wx.EXPAND | wx.LEFT, border=20)
        self.et_content = wx.TextCtrl(ctx, -1, value="", size=(300, 200), style=wx.TE_MULTILINE)
        contentBox.Add(self.et_content, 0, flag=wx.EXPAND | wx.LEFT, border=0)
        self.Add(contentBox, 0, wx.EXPAND | wx.TOP, border=10)

    def selectFile(self, e):
        wildcard = 'All files(*.mp4*)|*.*'
        dialog = wx.FileDialog(None, '选择视频文件', os.getcwd(), '', wildcard, wx.FD_OPEN)
        if dialog.ShowModal() == wx.ID_OK:
            # self.FileName.SetValue()
            print(dialog.GetPath())
            self.et_file.SetValue(dialog.GetFilename())
            dialog.Destroy()

    def getItemDict(self):
        title = self.et_title.GetValue()
        desc = self.et_content.GetValue()
        file = self.et_file.GetValue()
        contentDict = {"title": title, "describtion": desc}
        contentDict.get("")
        print("当前配置元组为",(file, contentDict))
        return (file, contentDict)

    def resetContent(self):
        self.et_title.SetValue("")
        self.et_content.SetValue("")
        self.et_file.SetValue("")
    def loadContent(self,item):
        self.et_title.SetValue(item[1].get("title"))
        self.et_content.SetValue(item[1].get("describtion"))
        self.et_file.SetValue(item[0])

    def setEdittingState(self):
        self.editing=True


app = wx.App()
frame = MainWindow(None, "FATRI-checktool")
frame.Show()
frame.Center(wx.BOTH)
app.MainLoop()
